<template>
  <div style="width:100%;height:auto; margin:0 auto;">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="上级节点">
        <el-select v-model="form.parentId" placeholder="请选择" disabled style="width:100%;">
          <el-option v-for="item in ggTypes" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="名称" />
      </el-form-item>
      <el-form-item label="排序号" prop="sortNum">
        <el-input v-model="form.sortNum" placeholder="排序号" />
      </el-form-item>
    </el-form>
    <div style="text-align:center; padding-top:10px">
      <el-button type="primary" @click="save('ruleForm')">保存</el-button>
      <el-button type="danger" @click="closePage">取消</el-button>
    </div>
  </div>
</template>

<script>
import { saveData, getAllLeafNode } from '@/api/dataDictoryCatalog'
import { Message } from 'element-ui'

export default {
  inject: ['getTreeList'],
  props: { // 第二种方式
    xiangmu: {
      type: String,
      required: true
    },
    classify: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {},
      ggTypes: [],
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getAllLeafNode()
  },
  methods: {
    getAllLeafNode() {
      getAllLeafNode({ xiangmuId: this.xiangmu }).then(response => {
        this.form.parentId = this.classify
        this.ggTypes = response.data
      }).catch(function() {
      })
    },
    save(ruleForm) { // 新增
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })

          this.form.xmId = this.xiangmu
          saveData(this.form).then(response => {
            Message({
              message: '新增成功',
              type: 'success',
              duration: 5 * 1000
            })

            this.$emit('update:visible', false)
            loading.close()
            this.getTreeList()
          }).catch(response => {
            loading.close()
            this.getList()
          })
        } else {
          return false
        }
      })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
